package com.lightsh.flashlight;

import com.lightsh.light.Light;
import com.lightsh.widget.VRatingBar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

public class BrightnessSetActivity extends Activity{

	
	private static final int MSG_FINISH_ACTIVITY = 1;
	private static final int MSG_UPDATE_BRIGHTNESS = 2;
	private static final int INTERVAL_FINISH_MSG_DELAY = 2000;
	
	private VRatingBar mRatingBar;
	private View mViewBg;
	private boolean mbAniming = false;
	private boolean mbShowing = false;
	
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
			case MSG_FINISH_ACTIVITY:
				finishActivity();
				break;
			case MSG_UPDATE_BRIGHTNESS:
				setBrightness(msg.arg1);
				break;
			}
			super.handleMessage(msg);
		}
		
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.brightness_select_layout);
		
		mViewBg = findViewById(R.id.viewBackground);
		
		mRatingBar = (VRatingBar) findViewById(R.id.ratingBar);
		mRatingBar.setMinRate(1);
		mRatingBar.setCurrentRate(LightSwitchWidget.getCur(this) + 1);
		mRatingBar.setOnRateChangeListener(new VRatingBar.OnRateChangeListener() {
			
			@Override
			public void onRateChange(VRatingBar ratingBar, int rate) {
				if(rate < 1 || rate > Light.MAX_BRIGHTNESS_LEVEL + 1){
					return;
				}
				rate -= 1;
				mHandler.obtainMessage(MSG_UPDATE_BRIGHTNESS, rate, rate).sendToTarget();
			}
		});
		
		mRatingBar.setOnTouchDownUpListener(new VRatingBar.OnTouchDownUpListener() {

			@Override
			public void onTouchDown(VRatingBar ratingBar) {
				deleteDelayMsg();
			}

			@Override
			public void onTouchUp(VRatingBar ratingBar) {
				setOrDelayFinishMsg();
				
			}

		});
		Animation anim = AnimationUtils.loadAnimation(this, R.anim.brightness_appear);
		mRatingBar.startAnimation(anim);
		
		anim = AnimationUtils.loadAnimation(this, R.anim.bg_appear);
		mViewBg.startAnimation(anim);
		
		setOrDelayFinishMsg();
	}
	private void setBrightness(int cur){
		LightSwitchWidget.saveCur(BrightnessSetActivity.this, cur);
		if(Light.isOpen()){
			Light.set(cur);
			LightSwitchWidget.updateAllViews(this);
		}
	}
	private void finishActivity(){
		if(mbAniming){
			return;
		}
		mbAniming = true;
		if(mbShowing){
			Animation anim = AnimationUtils.loadAnimation(this, R.anim.brightness_disappear);
			anim.setAnimationListener(new Animation.AnimationListener() {
				
				@Override
				public void onAnimationStart(Animation animation) {
				}
				
				@Override
				public void onAnimationRepeat(Animation animation) {
				}
				
				@Override
				public void onAnimationEnd(Animation animation) {
					BrightnessSetActivity.this.finish();
					
				}
			});
			mRatingBar.startAnimation(anim);
			anim = AnimationUtils.loadAnimation(this, R.anim.bg_disappear);
			mViewBg.startAnimation(anim);
		}else{
			BrightnessSetActivity.this.finish();
		}
	}
	
	private void setOrDelayFinishMsg(){
		if(mHandler.hasMessages(MSG_FINISH_ACTIVITY)){
			mHandler.removeMessages(MSG_FINISH_ACTIVITY);
		}
		mHandler.sendEmptyMessageDelayed(MSG_FINISH_ACTIVITY, INTERVAL_FINISH_MSG_DELAY);
	}
	private void deleteDelayMsg(){
		mHandler.removeMessages(MSG_FINISH_ACTIVITY);
	}
	

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		finishActivity();
		return true;
	}
	@Override
	protected void onResume() {
		mbShowing = true;
		super.onResume();
	}

	@Override
	protected void onPause() {
		mbShowing = false;
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		deleteDelayMsg();
		super.onDestroy();
	}
	
	@Override
	public void onBackPressed() {
		finishActivity();
	}
}
